package com.jh.netty.instruction;

import com.jh.netty.instruction.datapacket.ControllerHistoryResponseData;
import com.jh.netty.instruction.datapacket.DoorParam;

import java.util.Arrays;
import java.util.List;

/**
 * 测试指令
 *
 * 52 52 01 01 00 02 05 00 26 76    开门1
 * 52 52 01 01 00 02 05 01 36 57    开门2
 * 52 52 01 01 00 02 05 02 06 34    开门3
 * 52 52 01 01 00 02 05 03 16 15    开门4
 * 52 52 01 01 00 01 40 F0 62	读取门状态
 *          => 结果 52 52 01 01 00 11 40 01 01 01 01 01 01 01 01 01 01 01 01 04 04 04 04 04 ce
 *          => 结果 52 52 01 01 00 11 40 01 01 01 01 01 01 01 01 01 01 01 02 04 04 04 04 ea 1c
 * 52 52 01 01 00 01 06 d8 60   读取门参数
 *
 * 注意事项
 * 1、由于读卡器读卡距离不能满足10cm读卡距离，因此采用一个桌牌分配两张标签卡片。
 */
public class Main {

    /**
     * @param str 52520101000205002676
     * @return 52 52 01 01 00 02 05 00 26 76
     */
    public static String parse(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i+=2) {
            sb.append(str, i, i + 2).append(" ");
        }
        return sb.toString();
    }

    /**
     *
     * 测试
     */
    public static void main(String[] args) {
// 解析握手指令或者心跳指令
//        String insStr = "09 01 aa 00 3e 05 00 59 15 00";
//        String insStr = "03 01 98 00"
//        Instruction ins = Instruction.parse(insStr);
//        System.out.println(ins);

//// 解析指令
////        String ins = "5252010100a10600000002000000051e0000000000000000000012345678050000000000000000000000000000000000000002000000051e0000000000000000000012345678050000000000000000000000000000000000000002000000051e0000000000000000000012345678050000000000000000000000000000000000000002000000051e0000000000000000000012345678050000000000000000000000000000000087d4";
////        String ins = "52 52 01 01 00 02 05 00 26 76";
////        // CommunicationPacket(bootCode=5252, equipmentCode=01, controllerAddressCode=01, dataPacketLength=0002, dataPacket=DataPacket(cmdCode=05, dataCodeText=00), crc16Code=2676)
////        String ins = "52 52 01 01 00 02 05 01 36 57";
////        String ins = "52 52 01 01 00 11 40 01 01 01 01 01 01 01 01 01 01 01 01 04 04 04 04 04 ce";
////        String ins = "52 52 01 01 00 11 40 01 01 01 01 01 01 01 01 01 01 01 02 04 04 04 04 ea 1c";
////        String ins = "5252010100114001010101010101010201010104040404ccbb";
////        String ins = "52 52 01 01 00 01 84 69 aa";
////        String ins = "52 52 01 01 00 0c 11 00 1d 2c 90 48 21 01 00 aa 2b 83 5e 32";
////        String ins = "52 52 01 01 00 0C 11 00 1D 0B 01 B3 21 01 00 AA 3D 9F D1 5C";
//        String ins = "52 52 01 01 00 0c 11 00 1d 2c 97 f0 21 01 00 aa 3d a3 75 44";
//        String ins = "52 52 01 01 00 a1 06 00 00 00 02 00 00 00 05 1e 00 00 00 00 00 00 00 00 00 00 12 34 56 78 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00 00 00 05 1e 00 00 00 00 00 00 00 00 00 00 12 34 56 78 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00 00 00 05 1e 00 00 00 00 00 00 00 00 00 00 12 34 56 78 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00 00 00 05 1e 00 00 00 00 00 00 00 00 00 00 12 34 56 78 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 87 d4";
//        CommunicationPacket cp = CommunicationPacket.parse(ins);
//        System.out.println(cp);
//        System.out.println(cp.isValidCRC16());
//        List<DoorParam> doorParams = DoorParam.parse(cp.getDataPacket().getDataCodeText());
//        System.out.println(doorParams);

        String str = "001d2c97f0210100aa3da3";
        ControllerHistoryResponseData d = ControllerHistoryResponseData.parse(str);
        System.out.println(d);
        System.out.println(d.getDateTime());


//        System.out.println(CmdGetDotStatus.parse(cp.getDataPacket()));
//        CommunicationPacket scp = CommunicationPacket.parse("52 52 01 01 00 01 40 F0 62");
//        System.out.println(scp.toByteBuf());

////构造一个新的通讯包
//        CommunicationPacket cp1 = new CommunicationPacket();
//        cp1.setBootCode("5252");
//        cp1.setEquipmentCode("01");
//        cp1.setControllerAddressCode("01");
//        DataPacket dp1 = new DataPacket();
//        dp1.setCmdCode("11");
//        dp1.setDataCodeText("00");
//        cp1.setDataPacketLength(dp1.getDataPacketLength());
//        cp1.setDataPacket(dp1);
//        cp1.calCRC16();
//        String str = cp1.toInstructionText();
//        System.out.println("指令 = " + str);

//// 读取参数 (52 52 01 01 00 01 06 d8 60)
//        CommunicationPacket cp1 = new CommunicationPacket();
//        cp1.setBootCode("5252");
//        cp1.setEquipmentCode("01");
//        cp1.setControllerAddressCode("01");
//        DataPacket dp1 = new DataPacket();
//        dp1.setCmdCode("06");
//        dp1.setDataCodeText("");
//        cp1.setDataPacketLength(dp1.getDataPacketLength());
//        cp1.setDataPacket(dp1);
//        cp1.calCRC16();
//        String str = cp1.toInstructionText();
//        System.out.println("指令 = " + str);

//        其他测试
//        byte[] byteArr = new BigInteger(ins.replace(" ", ""), 16).toByteArray();
//        int c = CRC16.CRC16_CCITT_FALSE(byteArr);
////        String crc16Code = Integer.toHexString(c);
//        System.out.println(String.format("%04x", c));
//
//        // 5252 01 01000205002676
//        String bootCode = instrut.substring(0, 4);
//        String equipmentCode = instrut.substring(4, 6);
//        String controllerAddressCode = instrut.substring(6, 8);
//        String dataPacketLength = instrut.substring(8, 12);
//
//        String dataPacket = instrut.substring(12, instrut.length() - 4);
//        String crc16 = instrut.substring(bootCode.length() + equipmentCode.length() +  controllerAddressCode.length() +
//                dataPacketLength.length() + dataPacket.length());
//
//        CommunicationPacket cp = new CommunicationPacket();
//        cp.setBootCode(bootCode);
//        cp.setEquipmentCode(equipmentCode);
//        cp.setControllerAddressCode(controllerAddressCode);
//        cp.setDataPacketLength(dataPacketLength);
//        cp.setDataPacketInstruct(dataPacket);
//        cp.setCrc16Code(crc16);

//        System.out.println(cp);
//        byte[] bstr = new BigInteger("01 01 00 01 40".replace(" ", ""), 16).toByteArray();
//        int c = CRC16.CRC16_CCITT_FALSE(bstr);
//        System.out.println(c);
//        System.out.println(Integer.toHexString(c));

//        System.out.println(Arrays.toString(strings));
//        calCRC16(strings);
    }

    public static void calCRC16(String[] strings) {
        byte[] data = new byte[strings.length];
        for (int i = 0; i < strings.length; i++) {
            data[i] = Byte.parseByte(strings[i], 16);
        }
        int c = CRC16.CRC16_CCITT_FALSE(data);

        // 01 01 00 01 40
        System.out.println(Arrays.toString(data));
        System.out.println(c);
        System.out.println(Integer.toHexString(c));
    }
}
